package com.joyance.springmvc.util.json;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;

import com.joyance.springmvc.vo.Person;

public class HttpUtils {

    /**
     * httpclient get 请求
     * @param url 请求地址
     * @return json格式字符串  
     */
    public static String httpGET(String url) {
        StringBuilder json = new StringBuilder();
        CloseableHttpClient client = null;
        try {
            client = HttpClients.createDefault();
            HttpGet request = new HttpGet(url);
            CloseableHttpResponse response = client.execute(request);
            BufferedReader rd = null;
            if (response.getEntity() != null) {
                rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                String line = "";
                while ((line = rd.readLine()) != null) {
                    json.append(line);
                }
            }
            rd.close();
            response.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (client != null) {
                try {
                    client.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return json.toString();
    }


    /**
     * httpclient post 请求
     * @param url 请求地址
     * @return json格式字符串  
     */
    public static String httpPOST(String url, Map<String, String> params, String encode) {
        StringBuilder json = new StringBuilder();
        CloseableHttpClient client = null;
        try {
            client = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(url);
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(getParamList(params), encode);
            httpPost.setEntity(entity);
            CloseableHttpResponse response = client.execute(httpPost);
            BufferedReader rd = null;
            if (response.getEntity() != null) {
                rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                String line = "";
                while ((line = rd.readLine()) != null) {
                    json.append(line);
                }
            }
            rd.close();
            response.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (client != null) {
                try {
                    client.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return json.toString();
    }


    private static List<NameValuePair> getParamList(Map<String, String> map) {
        List<NameValuePair> parameters = new ArrayList<NameValuePair>();
        for (String key : map.keySet()) {
            parameters.add(new BasicNameValuePair(key, map.get(key)));
        }
        return parameters;
    }


    public static String getResponseContentType(String url) {
        CloseableHttpClient client = null;
        String contentType = null;
        try {
            client = HttpClients.createDefault();
            HttpGet request = new HttpGet(url);
            CloseableHttpResponse response = client.execute(request);
            contentType = response.getFirstHeader("Content-Type").getValue();
            response.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (client != null) {
                try {
                    client.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return contentType;
    }


    public static void main(String[] args) {
        Map<String, String> params = new LinkedHashMap<String, String>();
        Person person = new Person();
        person.setAge(11);
        person.setId(1L);
        person.setName("joy");

        Person person1 = new Person();
        person1.setAge(12);
        person1.setId(2L);
        person1.setName("joyance");
        List<Person> list = new ArrayList<Person>();
        list.add(person);
        list.add(person1);
        params.put("data", JsonUtils.writeObject(list));
        String result = HttpUtils.httpPOST("http://localhost:8080/springmvc/testHttp/testPostJsonArray.do", params, "UTF-8");
    }
}
